package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.example.common.dto.CommodityDTO;
import com.example.common.dto.UserDTO;
import com.example.domain.Commodity;
import com.example.domain.User;
import com.example.service.CommodityService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MainPageController {

	@Autowired
	private UserService userService;

	@Autowired
	private CommodityService commodityService;

	/**
	 * 获取购物中心所有轮播图商品
	 *
	 * @return 返回JSON字符串
	 */
	@PostMapping(value = "/mall/carousel/all", produces = "application/json;charset=utf-8")
	public String selectCommodityByCarousel() {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setIsCarousel(1).setAuditState(1).setProductState(0);
		return this.reConditionSearchCommodity(commodityDTO);
	}

	/**
	 * 根据商品名称查询商品信息（购物中心）
	 *
	 * @param text 商品名称
	 * @return 返回JSON字符串
	 */
	@PostMapping(value = "/mall/commodity/select/text", produces = "application/json;charset=utf-8")
	public String selectMallCommodityByText(String text) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setText(text).setAuditState(1).setProductState(0);
		return this.reConditionSearchMerchant(commodityDTO);
	}

	/**
	 * 用户查看所有店铺
	 *
	 * @return 返回所有店铺信息
	 */
	@PostMapping(value = "/mall/merchant/all", produces = "application/json;charset=utf-8")
	public String selectAllMerchant() {
		Map<String, Object> message = new HashMap<>(16);
		List<Map<String, Object>> resultList = new ArrayList<>();

		UserDTO userDTO = new UserDTO();
		userDTO.setAuditRegistryState(1).setEnableState(0).setIdentity(1);
		List<User> merchants = userService.getUserByCondition(userDTO);

		for (User merchant : merchants) {
			Map<String, Object> info = commodityService.getMerchantInfoForU(merchant.getId());
			if (info == null) {
				continue;
			}
			info.put("id", merchant.getId());
			info.put("restaurantName", merchant.getUserName());
			info.put("deliveryCost", 10);
			info.put("time", 15);
			info.put("distance", 100);
			resultList.add(info);
		}
		message.put("restaurants", resultList);
		message.put("total", resultList.size());
		return JSON.toJSONString(message);
	}

	/**
	 * 地图信息
	 *
	 * @return
	 */
	@PostMapping(value = "/restaurant/atlas", produces = "application/json;charset=utf-8")
	public String getMerchantMarker() {
		HashMap<String, Object> message = new HashMap<>(16);
		List<Map<String, Object>> resultList = new ArrayList<>();

		UserDTO userDTO = new UserDTO();
		userDTO.setIdentity(1);
		List<User> merchants = userService.getUserByCondition(userDTO);

		for (User merchant : merchants) {
			Map<String, Object> info = commodityService.getMerchantInfoForU(merchant.getId());
			if (info == null) {
				continue;
			}
			info.put("id", merchant.getId());
			info.put("restaurantName", merchant.getUserName());
			info.put("lng", merchant.getLongitude());
			info.put("lat", merchant.getLatitude());
			resultList.add(info);
		}
		message.put("markers", resultList);

		return JSON.toJSONString(message);
	}

	/**
	 * 返回条件查询所得商品数组
	 */
	private String reConditionSearchCommodity(CommodityDTO commodityDTO) {
		Map<String, Object> message = new HashMap<>(16);
		List<Commodity> commodities = commodityService.getCommodityByConForU(commodityDTO);
		List<Map<String, Object>> result = new ArrayList<>();
		for (Commodity commodity : commodities) {
			User merchant = userService.getById(commodity.getUserId());
			Map<String, Object> map = BeanUtil.beanToMap(commodity);
			map.put("restaurantName", merchant.getUserName());
			result.add(map);
		}
		message.put("commodities", result);
		message.put("total", commodities.size());

		return JSON.toJSONString(message);
	}

	/**
	 * 查询商店
	 *
	 * @param commodityDTO
	 * @return
	 */
	private String reConditionSearchMerchant(CommodityDTO commodityDTO) {
		Map<String, Object> message = new HashMap<>(16);
		List<Commodity> commodities = commodityService.getCommodityByConForU(commodityDTO);
		List<Map<String, Object>> result = new ArrayList<>();
		HashMap<Integer, Boolean> merchantIds = new HashMap<>(16);
		for (Commodity commodity : commodities) {
			if (!merchantIds.containsKey(commodity.getUserId())) {
				merchantIds.put(commodity.getUserId(), true);
			}
		}
		for (Integer id : merchantIds.keySet()) {
			User merchant = userService.getById(id);
			Map<String, Object> info = commodityService.getMerchantInfoForU(merchant.getId());
			if (info == null) {
				continue;
			}
			info.put("id", merchant.getId());
			info.put("restaurantName", merchant.getUserName());
			info.put("deliveryCost", 10);
			info.put("time", 15);
			info.put("distance", 100);
			result.add(info);
		}

		message.put("restaurants", result);
		message.put("total", result.size());
		return JSON.toJSONString(message);
	}
}
